package com.aier.school.action;

import java.io.File;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.common.common.CityUtil;
import com.aier.common.common.Constant;
import com.aier.common.common.ImageUtil;
import com.aier.school.entity.School;
import com.aier.school.entity.SchoolPhoto;
import com.aier.school.service.ISchoolService;

@Scope("prototype")
@Component("schoolCommonAction")
public class SchoolCommonAction extends SchoolBaseAction{

    /**
     * 学校公共元素
     * @param 学校管理-学校左侧栏页面与学校名称栏
     */
    private static final long serialVersionUID = 1L;
    
    @Resource(name = "schoolService")
    private ISchoolService schoolService;    

    private Integer sid;
    private Long tid;
    
    private String img;            //学校主图
    private File file;
    private String photoName; // 照片原名
       
    private int s;                //成功:1 ,失败:0
    private String error_info;    //错误提示信息
    
    private String selCoId;//默认图名称
    
    private School school;        //学校
    
    private List<SchoolPhoto> spList; // 学校相册图片列表
    
    @Override
    public String execute(){
        school = schoolService.searchSchoolBySid(sid);
        
        if (school.getCid() != null) {
            school.setAddress(CityUtil.getCityListToString(school.getCid()));
        }
        
        return SUCCESS;
    }
    
    //上传学校封面图片
    public String updateSchoolImg(){
        img = ImageUtil.uploadImageAdjustable(file, schoolimg_upload_url,
                photoName,Constant.num_269,Constant.num_82,Constant.num_980,Constant.num_300,true);
        
        return SUCCESS;
    }    
    
    
    /**
     * 删除学校封面图片
     * @return
     */
    public String deleteImage() {
        File fo = new File(schoolimg_upload_url + photoName);
        if (fo.exists()) {
            fo.delete();
        }
        s = 1;
        return SUCCESS;
    }
    
    
    //将学校封面图片名存入数据库
    public String updateSImgName(){
        if(schoolService.updateSchoolImg(sid, img)==0){
            s = 0;
            error_info = "修改学校封面失败";
        }
        s = 1;
        return SUCCESS;
    }
    
    //设置给定图片为封面
    public String updateGivImgName() {
          
        if (schoolService.updateSchoolImg(sid, selCoId+".jpg") == 0) {
            s = 0;
            error_info = "修改学校封面失败";
        }
        s = 1;
        return SUCCESS;
    }
    
    
    //读取学校封面图片
    public String serachSchoolimg() {
        School s = schoolService.searchSchoolBySid(sid);
        if (null != s) {
            img = s.getImg();
        }
        return SUCCESS;
    }
    
    //读取学校风采
    public String serachCampusStyle() {
        spList = schoolService.searchSchoolPhotoList(sid);
        
        return SUCCESS;
    }

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public Long getTid() {
        return tid;
    }

    public void setTid(Long tid) {
        this.tid = tid;
    }

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public void setPhotoName(String photoName) {
        this.photoName = photoName;
    }

    public void setSchoolService(ISchoolService schoolService) {
        this.schoolService = schoolService;
    }

    public int getS() {
        return s;
    }

    public String getError_info() {
        return error_info;
    }


    public String getSelCoId() {
        return selCoId;
    }

    public void setSelCoId(String selCoId) {
        this.selCoId = selCoId;
    }

    public School getSchool() {
        return school;
    }

    public List<SchoolPhoto> getSpList() {
        return spList;
    }

}
